<?php
class Weixinapi extends Base_Controller
{
	private $_appid;
	private $_appsecret;
	private $_wx_type;

	public function __construct()
	{
		parent::__construct();
        $this->config->load('weixin');
        $this->_appid = $this->config->item('nldou_fuwu_appid');
        $this->_appsecret = $this->config->item('nldou_fuwu_appsecret');
        $this->_wx_type = $this->config->item('nldou_fuwu_type');
	}

	private function getSourceConfig($source){
        if (!empty($source)){
            $this->config->load('weixin');
            $this->_appid = $this->config->item($source.'_appid');
            $this->_appsecret = $this->config->item($source.'_appsecret');
            $this->_wx_type = $this->config->item($source.'_type');
        }

    }

    //被动回复消息
    /**
     * @param string $source 公众号类型
     *
     */
    public function response_msg($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library('wxinterface/Wxcallbackapi',array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));

        if (!isset($_GET['echostr'])) {
            $result=$this->wxcallbackapi->responseMsg();
            echo $result;
            exit;
        }else{
            $this->wxcallbackapi->valid();
        }
    }
	
	//查询菜单设置
    public function get_menu_info($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
    	$res=$this->wxinterface->get_menu_info();
    	$this->success($res);
    }
    
    //设置菜单
    public function set_menu($source = '')
    {
        $this->getSourceConfig($source);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
    	$button='{"button":['.
            '{"name":"能量逗","sub_button":['.
            '{"type":"click","name":"加入我们","key":"V1_JOIN_US"},'.
            '{"type":"click","name":"关于我们","key":"V1_ABOUT_US"},'.
    	    '{"type":"view","name":"精彩回顾","url":"http://mp.weixin.qq.com/mp/homepage?__biz=MzA3MTQ5ODAxNA==&hid=1&sn=91bbb626c0aa717ce1848c38baadfce7#wechat_redirect"},'.
    	    '{"type":"view","name":"卡牌规则","url":"http://wap.nldou.com/hardfun"}]},'.
		    '{"type":"view","name":"卡牌规则","url":"http://wap.nldou.com/hardfun"},'.
		    '{"name":"商城","sub_button":['.
    	    '{"type":"view","name":"成为代言人","url":"https://h5.koudaitong.com/v2/trade/directseller/tutorial.html?kdt_id=18168297"},'.
    		'{"type":"view","name":"推广海报","url":"https://wap.koudaitong.com/salesman/poster/poster/index?kdt_id=18168297"},'.
            '{"type":"view","name":"能量商城","url":"https://h5.koudaitong.com/v2/feature/1ekqipm0r"}]}]}';
    	$res=$this->wxinterface->set_menu($button);
    	$this->success($res);
    }
    
    //js sdk初始化验证
    public function init_wx_jsapi($source = '')
    {
    	if(isset($_GET['from_url'])){
    		$from_url = $_GET['from_url'];
    	}else{
    		$this->error('缺少参数from_url');
    	}
    	$this->getSourceConfig($source);
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
    	//签名
    	$init['signature'] = $this->wxinterface->getJsapiSignature($from_url);
    	//签名使用的随机串
    	$init['nonceStr'] = $this->wxinterface->getJsapiNonceStr();
    	//签名使用的时间戳
    	$init['timestamp'] = $this->wxinterface->getJsapiTimestamp();
    	 
    	$init['appid'] = $this->wxinterface->getAppid();
    	 
    	$this->success($init);
    }
    

    //发送客服消息
    public function send_custom_msg($source = '', $data = ''){
	    if (!empty($data)){
	        $this->getSourceConfig($source);
            $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
            $res=$this->wxinterface->send_custom_msg($data);
            var_dump($res);
        }else{
	        $this->error("参数不全");
        }
    }
    
    
}